Working with APIs in R

Code Meets Culture

Hafeezul Raziq

April 19, 2025

What is API?

What is an API?

  • API = Application Programming Interface
  • Like a waiter in a restaurant: you (the client) order, kitchen (the server) prepares, waiter (API) returns the dish (data)
  • Most APIs return responses in JSON format

Types of API

Types of API

  • There are many ways to categorize APIs, but from a usage perspective, we can broadly group them into two types:

1. Data Retrieval APIs 📥

  • Also called READ APIs
  • Used to get information from a service

2. Control / Action APIs 🛠️

  • Also known as Write or Command APIs
  • Used to perform actions or control something remotely

🔍 Focus of This Session

  • I’ll be focusing on the first type: Data Retrieval APIs — where we ask a service for data and use that for analysis or visualization

How API Calls Work

  1. Request – You call a URL with query parameters
  2. Response – You get structured data (usually JSON)
  3. Parsing – Use R packages like httr and jsonlite to extract data
library(httr)
library(jsonlite)

res <- GET("https://api.example.com/data", query = list(param = "value"))

data <- fromJSON(content(res, "text"))

httr logo jsonlite logo

API Keys

API: Keyless vs With Key

display: flex; justify-content: center; align-items: center; height: 60vh; text-align: center; font-size: 1.5em; “}
Feature No Key Required API Key Required
Setup Needed None Yes (Google Cloud, etc.)
Rate Limits Often lower Higher limits
Use Case Open data, testing Production, private APIs

Keyless API in R

Accessing a Keyless API in R

Example: Quran API (English Translation)

library(httr)
library(jsonlite)

url <- "https://cdn.jsdelivr.net/gh/fawazahmed0/quran-api@1/editions/eng-muhammadtaqiudd.json"

quran_api <- GET(url)

quranJSON <- content(quran_api, as = "text")

quran <- fromJSON(quranJSON)$quran

Viz: Bar chart of top 99 most frequently occured words in Al-Quran (Eng Translation)

library(ggplot2)
library(dplyr)
library(viridis)

top_words <- word_freq |> 
  slice_max(n, n = 99)

# Reorder words for better plot
ggplot(top_words, aes(x = n, y = reorder(word, -n), fill = n)) +
  geom_col() +
  scale_fill_viridis(option = "D", direction = -1) +
  coord_flip() +
  labs(
    title = "Top 99 Most Frequent Words in the Quran (English Translation)",
    x = "Frequency",
    y = "Word"
  ) +
  theme_minimal() +
  theme(
    axis.text.x = element_text(angle = 90, hjust = 1),
    legend.position = "none"
  )

Bar chart (cont.)

Viz: Word cloud of top 99 most frequently occurred words in Al-Quran (Eng Translation)

library(wordcloud)

wordcloud(
  words = top_words$word,
  freq = top_words$n,
  max.words = 99,
  random.order = FALSE,
  colors = brewer.pal(8, "Dark2")
)

API with Key in R

YouTube Data API (With Key)

We used {httr} for flexibility, but there’s also a special R package for YouTube: {tuber} package

{tuber} is a simple wrapper for the YouTube Data API — it makes things easier!

Some key functions include: yt_search() - search for YouTube videos, get_stats() – get video stats like views, likes, and comments and many more.

Authenticating with YouTube API

To use the YouTube Data API, first set your credentials in .Renviron:

Then, in your R script:

app_id <- Sys.getenv("YOUTUBE_APP_ID")
app_secret <- Sys.getenv("YOUTUBE_APP_SECRET")

# Authenticate
yt_oauth(app_id, app_secret, token = "")

⚠️ Note: Some Google APIs (like Google Maps) are paid, so always check pricing to avoid unexpected bills!

# Search for Hari Raya Official Music Video
results <- yt_search("Hari Raya Official Music Video", type = "video", max_results = 50)

# Get stats for each video
stats_list <- lapply(results$video_id, function(id) get_stats(video_id = id))
stats_df <- map_df(stats_list, ~as.data.frame(t(unlist(.x))), .id = "index")
     video_id          publishedAt                channelId
1 67p1sOD4e2A 2024-03-08T07:00:08Z UCIxxcbRgejg8scgiqF390jw
2 zf1U4mu0Iew 2016-07-01T13:00:03Z UCSUWBtaz8MVTFy9GULySSmA
3 k5RawzRXeVE 2021-04-09T12:30:15Z UCe05yXcSHjcgZ4EeV8fs_sA
4 4vtwJORy_eU 2013-05-25T01:56:20Z UCBd5pENmJrvi6PQq-2ndBhw
5 seZsq1mD0uk 2021-04-30T12:00:13Z UCAyzn7DznYgmbvvXJ4bC83Q
6 EA_BrwGx8S0 2025-03-19T03:11:20Z UC4Zs5RInEZ2dTBEuvSQvBkg
                                                            title
1                Alamak Raya Lagi - De Fam (Official Music Video)
2   Nyanyian Ramai - Warna Warni Aidilftri (Official Music Video)
3       Iman Troye - Pejam Celik Hari Raya (Official Music Video)
4 Siti Nurhaliza - Bila Hari Raya Menjelma (Official Music Video)
5                   DOLLA - Raya Raya Raya (Official Music Video)
6                       MIMIFLY - SERUMPUN (Official Music Video)
                                                                                                                       description
1       LISTEN TO ALAMAK RAYA LAGI: https://Faithful.lnk.to/AlamakRayaLagi Maxis Caller Ringtones: Dial *131*774391# and press ...
2    Nyanyian Ramai - Warna Warni Aidilftri Composer - Lan Laga [Universal Music Publishing (M) Sdn Bhd] Lyricist - Amran Omar ...
3  XO House Presents : Iman Troye - Pejam Celik Hari Raya (Official Music Video) Executive Producer = Adib Hamdi Song Producer ...
4             Anggun si dara jelita berselindang dan kebaya Sepadan dengan pemuda tampan dan segak orangnya Di pagi hari mulia ...
5 Stream 'Raya Raya Raya' on all digital streaming platforms: https://dolla.lnk.to/RayaRayaRaya Download RBT now! Maxis Caller ...
6     Composer : Mimifly, Cuurley Author : Mimifly, Cuurley Publisher : Taja Archive Sdn Bhd & 153Joombas Master P&C 2024 MFLY ...
                          thumbnails.default.url thumbnails.default.width
1 https://i.ytimg.com/vi/67p1sOD4e2A/default.jpg                      120
2 https://i.ytimg.com/vi/zf1U4mu0Iew/default.jpg                      120
3 https://i.ytimg.com/vi/k5RawzRXeVE/default.jpg                      120
4 https://i.ytimg.com/vi/4vtwJORy_eU/default.jpg                      120
5 https://i.ytimg.com/vi/seZsq1mD0uk/default.jpg                      120
6 https://i.ytimg.com/vi/EA_BrwGx8S0/default.jpg                      120
  thumbnails.default.height                            thumbnails.medium.url
1                        90 https://i.ytimg.com/vi/67p1sOD4e2A/mqdefault.jpg
2                        90 https://i.ytimg.com/vi/zf1U4mu0Iew/mqdefault.jpg
3                        90 https://i.ytimg.com/vi/k5RawzRXeVE/mqdefault.jpg
4                        90 https://i.ytimg.com/vi/4vtwJORy_eU/mqdefault.jpg
5                        90 https://i.ytimg.com/vi/seZsq1mD0uk/mqdefault.jpg
6                        90 https://i.ytimg.com/vi/EA_BrwGx8S0/mqdefault.jpg
  thumbnails.medium.width thumbnails.medium.height
1                     320                      180
2                     320                      180
3                     320                      180
4                     320                      180
5                     320                      180
6                     320                      180
                               thumbnails.high.url thumbnails.high.width
1 https://i.ytimg.com/vi/67p1sOD4e2A/hqdefault.jpg                   480
2 https://i.ytimg.com/vi/zf1U4mu0Iew/hqdefault.jpg                   480
3 https://i.ytimg.com/vi/k5RawzRXeVE/hqdefault.jpg                   480
4 https://i.ytimg.com/vi/4vtwJORy_eU/hqdefault.jpg                   480
5 https://i.ytimg.com/vi/seZsq1mD0uk/hqdefault.jpg                   480
6 https://i.ytimg.com/vi/EA_BrwGx8S0/hqdefault.jpg                   480
  thumbnails.high.height       channelTitle liveBroadcastContent
1                    360           FAITHFUL                 none
2                    360      Sony Music MY                 none
3                    360           XO HOUSE                 none
4                    360 SuriaRecords (SRC)                 none
5                    360  DOLLAofficialVEVO                 none
6                    360            Mimifly                 none
           publishTime index
1 2024-03-08T07:00:08Z     1
2 2016-07-01T13:00:03Z     2
3 2021-04-09T12:30:15Z     3
4 2013-05-25T01:56:20Z     4
5 2021-04-30T12:00:13Z     5
6 2025-03-19T03:11:20Z     6
# Data Cleaning

laguhariraya <- stats_df |>
  left_join(results |> mutate(index = as.character(row_number())), by = "index") |>
  mutate(
    index = as.numeric(index),
    viewCount = as.numeric(viewCount),
    likeCount = as.numeric(likeCount),
    commentCount = as.numeric(commentCount),
    video_id = video_id,
    publishTime = as.POSIXct(publishTime, format = "%Y-%m-%dT%H:%M:%SZ", tz = "UTC")
  ) |>
  filter(
    !grepl("shorts", tolower(title)),
    !grepl("shorts", tolower(description)),
    publishTime <= as.POSIXct("2025-03-31", tz = "UTC")
  ) |>
  select(
    -favoriteCount,
    -starts_with("thumbnails.default"),
    -starts_with("thumbnails.medium"),
    -starts_with("thumbnails.high"),
    -liveBroadcastContent 
  )

laguhariraya <- laguhariraya %>%
  filter(!title %in% c(
    "Ngakak 🤣🤣🤣",
    "Upin &amp; Ipin hari raya🇲🇾",
    "Harry - Suaramu Syairku (Official Music Video)",
    "Rajini Murugan Tamil Full Movie - Sivakarthikeyan | Keerthy Suresh | D.Imman | UIE Movies",
    "Iman Troye - Teman (Official Music Video)",
    "Selamat Hari Raya Idul Fitri 1443  Hijriyah 2022 🙏🙏",
    "Selamat Hari Raya!",
    "Tahun ni nak dondon lagi tak yeeee? #PapaPipi #InspirasiRaya #SembangRaya #SembangEntertainment",
    "kartun lawak kampus raya 2023",
    "takbir | takbir keliling 2023 | hari raya idul fitri 2023 | takbiran",
    "DISCO HUNTER - Senandung Hari Raya",
    "persiapan hari raya idul fitri 2023 tunggu hari mainnya #short #takbiran #desakutuk",
    "PETRONAS Raya 2025: Anak Siapa Ni? | Who Could That Be?"
  ))
  index          id viewCount likeCount commentCount    video_id
1     1 67p1sOD4e2A  56135930    198822         9313 67p1sOD4e2A
2     2 zf1U4mu0Iew  18123175     44014         3381 zf1U4mu0Iew
3     3 k5RawzRXeVE  30995492    228227        14337 k5RawzRXeVE
4     4 4vtwJORy_eU  22446141     58027         4109 4vtwJORy_eU
5     5 seZsq1mD0uk  35572591    191576       112322 seZsq1mD0uk
6     6 EA_BrwGx8S0   8877764    126955         7392 EA_BrwGx8S0
           publishedAt                channelId
1 2024-03-08T07:00:08Z UCIxxcbRgejg8scgiqF390jw
2 2016-07-01T13:00:03Z UCSUWBtaz8MVTFy9GULySSmA
3 2021-04-09T12:30:15Z UCe05yXcSHjcgZ4EeV8fs_sA
4 2013-05-25T01:56:20Z UCBd5pENmJrvi6PQq-2ndBhw
5 2021-04-30T12:00:13Z UCAyzn7DznYgmbvvXJ4bC83Q
6 2025-03-19T03:11:20Z UC4Zs5RInEZ2dTBEuvSQvBkg
                                                            title
1                Alamak Raya Lagi - De Fam (Official Music Video)
2   Nyanyian Ramai - Warna Warni Aidilftri (Official Music Video)
3       Iman Troye - Pejam Celik Hari Raya (Official Music Video)
4 Siti Nurhaliza - Bila Hari Raya Menjelma (Official Music Video)
5                   DOLLA - Raya Raya Raya (Official Music Video)
6                       MIMIFLY - SERUMPUN (Official Music Video)
                                                                                                                       description
1       LISTEN TO ALAMAK RAYA LAGI: https://Faithful.lnk.to/AlamakRayaLagi Maxis Caller Ringtones: Dial *131*774391# and press ...
2    Nyanyian Ramai - Warna Warni Aidilftri Composer - Lan Laga [Universal Music Publishing (M) Sdn Bhd] Lyricist - Amran Omar ...
3  XO House Presents : Iman Troye - Pejam Celik Hari Raya (Official Music Video) Executive Producer = Adib Hamdi Song Producer ...
4             Anggun si dara jelita berselindang dan kebaya Sepadan dengan pemuda tampan dan segak orangnya Di pagi hari mulia ...
5 Stream 'Raya Raya Raya' on all digital streaming platforms: https://dolla.lnk.to/RayaRayaRaya Download RBT now! Maxis Caller ...
6     Composer : Mimifly, Cuurley Author : Mimifly, Cuurley Publisher : Taja Archive Sdn Bhd & 153Joombas Master P&C 2024 MFLY ...
        channelTitle         publishTime year
1           FAITHFUL 2024-03-08 07:00:08 2024
2      Sony Music MY 2016-07-01 13:00:03 2016
3           XO HOUSE 2021-04-09 12:30:15 2021
4 SuriaRecords (SRC) 2013-05-25 01:56:20 2013
5  DOLLAofficialVEVO 2021-04-30 12:00:13 2021
6            Mimifly 2025-03-19 03:11:20 2025

Top 10 Most Viewed Videos

laguhariraya %>%
  arrange(desc(viewCount)) %>%
  select(title = title, viewCount) %>%
  head(11)
                                                                            title
1  Lagu Raya | Siti Nordiana x Omar &amp; Hana | Oh Meriahnya Raya! | Video Rasmi
2                                Alamak Raya Lagi - De Fam (Official Music Video)
3                            Upin &amp; Ipin - Suasana Hari Raya [Sing-Along][HD]
4                                   DOLLA - Raya Raya Raya (Official Music Video)
5               Floor 88 &amp; Baby Shima - Tak Dapat Raya (Official Music Video)
6                       Iman Troye - Pejam Celik Hari Raya (Official Music Video)
7                           Siti Nurhaliza - Nazam Lebaran (Official Music Video)
8                         Siti Nurhaliza - Air Mata Syawal (Official Music Video)
9                               NABILA RAZALI - RAYA MANA? [OFFICIAL MUSIC VIDEO]
10                                       Balik Kampung Lagi #puasa #animasi #raya
11                Siti Nurhaliza - Bila Hari Raya Menjelma (Official Music Video)
   viewCount
1   61970228
2   56135930
3   36157718
4   35572591
5   33519633
6   30995492
7   26399393
8   25870601
9   24893746
10  24164063
11  22446141

Top 10 Most Liked Videos

laguhariraya %>%
  arrange(desc(likeCount)) %>%
  select(title = title, likeCount) %>%
  head(10)
                                                                                           title
1  Kalau kalian kegiatan nya di hari raya ngapain aja nih,komen!! #shrots #lucu #komedi #lebaran
2                                                       Balik Kampung Lagi #puasa #animasi #raya
3                                      Iman Troye - Pejam Celik Hari Raya (Official Music Video)
4                              Floor 88 &amp; Baby Shima - Tak Dapat Raya (Official Music Video)
5                                               Alamak Raya Lagi - De Fam (Official Music Video)
6                                                  DOLLA - Raya Raya Raya (Official Music Video)
7                                    ECKO SHOW - Takbirap (feat. BOSSVHINO, AIL) [ Music Video ]
8                                  Hael Husaini - Bersyukur Seadanya [Official Raya Music Video]
9                                           Upin &amp; Ipin - Suasana Hari Raya [Sing-Along][HD]
10                                                     MIMIFLY - SERUMPUN (Official Music Video)
   likeCount
1     607313
2     466882
3     228227
4     223064
5     198822
6     191576
7     157648
8     139022
9     131031
10    126955

Top 10 Most Commented Videos

laguhariraya %>%
  arrange(desc(commentCount)) %>%
  select(title = title, commentCount) %>%
  head(10)
                                                                                              title
1                                                     DOLLA - Raya Raya Raya (Official Music Video)
2               Dato Sri Aliff Syukri &amp; Datin Sri Shahida - Kelepok Raya [Official Music Video]
3                              Hael Husaini &amp; DOLLA - Suara Lebaran Kita (Official Music Video)
4                                         Iman Troye - Pejam Celik Hari Raya (Official Music Video)
5           Dato Sri Aliff Syukri - Chu Ku Chuk Raya [Official Music Video]  Penyanyi No 1 Malaysia
6                                                  Alamak Raya Lagi - De Fam (Official Music Video)
7                           Dato&#39; Sri Siti Nurhaliza - Raya Nak Ke Mana? (Official Music Video)
8                                           Siti Nurhaliza - Air Mata Syawal (Official Music Video)
9                                     Hael Husaini - Bersyukur Seadanya [Official Raya Music Video]
10 Menanti Di Aidilfitri - Wany Hasrita, Wani Syaz, Muna Shahirah, Wan Azlyn (Official Music Video)
   commentCount
1        112322
2         19263
3         17583
4         14337
5         10471
6          9313
7          8761
8          8724
9          8640
10         7878

Views vs Likes

Pearson Correlation ≈ 0.93
A strong positive relationship — as views increase, likes increase proportionally.

Likes vs Comments

Pearson Correlation ≈ 0.33
There’s a moderate positive relationship — more liked videos tend to receive more comments, but the relationship is weaker than views vs likes.

Views vs Comments

Pearson Correlation ≈ 0.40
There is a moderate relationship — videos with higher views tend to have more comments, though not as strongly correlated as likes and views.

Top Viewed Hari Raya Songs by Year (2009–2025)

The most viewed song released in each year:

  • 2009: Raihan & Hujan – Salam Aidilfitri Ayahanda Dan Bonda — 2.3M views
  • 2010: Suasana Hari Raya – Anuar Zain & Ellina — 13.5M views
  • 2012: Keindahan Aidilfitri – Nana, Syura, Achik, etc. — 8.9M views
  • 2013: Siti Nurhaliza – Nazam Lebaran — 26.4M views
  • 2014: Upin & Ipin – Suasana Hari Raya — 3.6M views
  • 2015: Upin & Ipin – Selamat Hari Raya — 1.3M views
  • 2016: Nyanyian Ramai – Warna Warni Aidilfitri — 18.1M views
  • 2017: Sufian Suhaimi – Kasih Lebaran — 10.2M views
  • 2018: Siti Nordiana x Omar & Hana – Lagu Raya61.9M views 🔥
  • 2019: Floor 88 & Baby Shima – Tak Dapat Raya — 33.5M views
  • 2020: Khalifah – Salam Aidil Fitri — 16.9M views
  • 2021: DOLLA – Raya Raya Raya — 35.6M views
  • 2022: Hael Husaini & DOLLA – Suara Lebaran Kita — 5.0M views
  • 2023: AINA ABDUL – BERKUNJUNG DI HARI RAYA — 4.3M views
  • 2024: De Fam – Alamak Raya Lagi — 5.6M views

Top 1 Hari Raya Song (2025)

Meriah Lain MacamHael Husaini & Nadeera
📺 Rocketfuel Network
👁️‍🗨️ 1,335,263 views · 👍 87,020 · 💬 5,533

What Else Can You Do With APIs?

  • 🌤 Weather forecasting (Open-Meteo)
  • 🎶 Spotify song data (spotifyr)
  • 📰 News headlines (GNews API)
  • 🐦 Social media analytics (Twitter, TikTok)
  • 📊 Government datasets (e.g., data.gov)

📦 Combine API data with R for storytelling and dashboards

Resources & Learning More

  • Postman: test APIs easily
  • Free-APIs: explore APIs
  • Public APIs: explore public APIs
  • R Packages: httr, jsonlite, tuber, spotifyr
  • Youtube or API Documentations

Thank You 🙏

Code Meets Culture: Hari Raya Music Meets R

💬 Any questions?